﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

public class EnemyInfo{
	public int id;
	public int spawn;
	public double delay;
	public bool isFire;

	public EnemyInfo(){
		id = 0;
		spawn = 0;
		delay = 0.0f;
		isFire = false;
	}
}

public class EnemyWave{
	public double delay;
	public List<EnemyInfo> enemy_list;
	public bool isAllFire;

	public EnemyWave(){
		delay = 0.0f;
		isAllFire = false;
		enemy_list = new List<EnemyInfo> ();
	}
}

public class LevelInfo {
	public List<EnemyWave> waves_list;

	public LevelInfo(){
		waves_list = new List<EnemyWave> ();
	}
}

public class LevelObject : MonoBehaviour {

	public TextAsset txtAssetFile;
	public LevelInfo levelInfo;

	// Use this for initialization
	void Start () {
		levelInfo = new LevelInfo ();
		parserLevel ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void parserLevel(){

		JsonData jsonData = JsonMapper.ToObject (txtAssetFile.text);

		JsonData jsonLevel = jsonData ["level"];
		JsonData jsonWaves = jsonLevel ["waves"];

		for(int i=0;i<jsonWaves.Count;i++){
			JsonData jsonWave = jsonWaves[i];

			double delayTime = double.Parse(jsonWave["delay"].ToString());
			JsonData enemysInfo = jsonWave["enemys"];

			EnemyWave waveInfo = new EnemyWave();
			waveInfo.delay = delayTime;

			for(int j = 0; j<enemysInfo.Count;j++){
				JsonData jsonEnemy = enemysInfo[j];
				int enemyId = int.Parse(jsonEnemy["id"].ToString());
				int spawnId = int.Parse(jsonEnemy["spawn"].ToString());
				double delayTimeEnemy = double.Parse(jsonEnemy["delay"].ToString()); 

				EnemyInfo enemyInfo = new EnemyInfo();
				enemyInfo.id = enemyId;
				enemyInfo.spawn = spawnId;
				enemyInfo.delay = delayTimeEnemy;
				waveInfo.enemy_list.Add(enemyInfo);
			}
			levelInfo.waves_list.Add(waveInfo);
		}
	}
}
